package flc.ast.fragment;

import static android.content.Context.INPUT_METHOD_SERVICE;
import static androidx.core.content.ContextCompat.getSystemService;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CollectActivity;
import flc.ast.activity.HighScoreActivity;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.HomeScoreAdapter;
import flc.ast.adapter.TodayRecAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private TodayRecAdapter recAdapter;
    private HomeScoreAdapter scoreAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivHighMore.setOnClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 5));
        recAdapter = new TodayRecAdapter();
        mDataBinding.rvRecommendList.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(this);

        mDataBinding.rvHighScoreList.setLayoutManager(new GridLayoutManager(mContext, 2));
        scoreAdapter = new HomeScoreAdapter();
        mDataBinding.rvHighScoreList.setAdapter(scoreAdapter);
        scoreAdapter.setOnItemClickListener(this);

        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    SearchActivity.key = key;
                    startActivity(SearchActivity.class);
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCollect://收藏
                startActivity(CollectActivity.class);
                break;
            case R.id.ivSearch://搜索
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_key));
                    return;
                }
                SearchActivity.key = key;
                startActivity(SearchActivity.class);
                break;
            case R.id.ivHighMore://高分大片酣畅看
                startActivity(HighScoreActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getRecommendData();
        getHighScoreData();
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(null, AppConfig.TODAY_RECOMMEND_DATA, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getHighScoreData() {
        StkResApi.getTagResourceList(null, AppConfig.HIGH_SCORE_DATA, StkResApi.createParamMap(0, 8), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        scoreAdapter.setList(result);
                    }
                }
            }
        });
    }

}